<template>
  <div class="app-container">
    <!-- 查询部分 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="姓名" prop="nickName">
        <el-input v-model="queryParams.nickName" placeholder="请输入姓名" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="班级ID" prop="classId">
        <el-input v-model="queryParams.classId" placeholder="请输入班级ID" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="学号" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入学号" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="实验名称" prop="expId">
      <el-autocomplete v-model="tem.expName" :fetch-suggestions="querySearch" placeholder="请输入实验名称" @select="handleSelect">
        <template slot-scope="{ item }">
          <div style="display: flex;" >
            <div class="name">{{ item.name }}</div>
            <span class="addr">{{ item.id}}</span>
          </div>

        </template>
      </el-autocomplete>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <!-- 右边工具栏 -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-form>

    <el-table v-loading="loading" :data="reportList" @selection-change="handleSelectionChange">
      <!-- 显示的数据 -->
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="id" align="center" prop="id" v-if="true" />
      <el-table-column label="姓名" width="80" align="center" prop="nickName" />
      <el-table-column label="学号" width="180" align="center" prop="userName" />
      <!-- <el-table-column label="实验名称" align="center" prop="expId" /> -->
      <el-table-column label="实验名称" align="center" prop="expId">
        <template slot-scope="scope">
          <div>{{idToName(scope.row.expId)}}</div>
        </template>
      </el-table-column>
      <el-table-column label="实验pdf" align="center" prop="submitUrl">
        <template slot-scope="scope">
          <el-button size="mini" @click="openurl(scope.row.submitUrl)">查看</el-button>
        </template>
      </el-table-column>

      <el-table-column sortable label="实验目的成绩" align="center" prop="scorePurpose" />
      <el-table-column sortable label="实验仪器用具成绩" align="center" prop="scoreTool" />
      <el-table-column sortable label="实验原理成绩" align="center" prop="scorePrinciple" />
      <el-table-column sortable label="实验内容成绩" align="center" prop="scoreContent" />
      <el-table-column sortable label="实验数据处理成绩" align="center" prop="scoreData" />
      <el-table-column sortable label="实验现象及分析成绩" align="center" prop="scoreSum" />
      <el-table-column sortable label="最终实验报告成绩" align="center" prop="scoreFinal" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="Updatescore(scope.row)">打分</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改用户实验报告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="学生id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入学生id" />
        </el-form-item>
        <el-form-item label="实验id" prop="expId">
          <el-input v-model="form.expId" placeholder="请输入实验id" />
        </el-form-item>
        <el-form-item label="实验目的成绩" prop="scorePurpose">
          <el-input v-model="form.scorePurpose" placeholder="请输入实验目的成绩" />
        </el-form-item>
        <el-form-item label="实验仪器用具成绩" prop="scoreTool">
          <el-input v-model="form.scoreTool" placeholder="请输入实验仪器用具成绩" />
        </el-form-item>
        <el-form-item label="实验原理成绩" prop="scorePrinciple">
          <el-input v-model="form.scorePrinciple" placeholder="请输入实验原理成绩" />
        </el-form-item>
        <el-form-item label="实验内容成绩" prop="scoreContent">
          <el-input v-model="form.scoreContent" placeholder="请输入实验内容成绩" />
        </el-form-item>
        <el-form-item label="实验数据处理成绩" prop="scoreData">
          <el-input v-model="form.scoreData" placeholder="请输入实验数据处理成绩" />
        </el-form-item>
        <el-form-item label="实验现象及分析成绩" prop="scoreSum">
          <el-input v-model="form.scoreSum" placeholder="请输入实验现象及分析成绩" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listReport,
    getReport,
    delReport,
    addReport,
    updateReport,
    expReportGetScore,
    getallExp
  } from "@/api/system/report";

  export default {
    name: "Report",
    data() {
      return {
        tem:{
          expName:""
        },
        allexps:[],


        // 按钮loading
        buttonLoading: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 用户实验报告表格数据
        reportList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          nickName: undefined,
          userName: undefined,
          expId: undefined,
          classId: undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          expId: [{
            required: true,
            message: "不能为空",
            trigger: "blur"
          }],
          userId: [{
            required: true,
            message: "不能为空",
            trigger: "blur"
          }],
          scoreContent: [{
            required: true,
            message: "不能为空",
            trigger: "blur"
          }],
          scoreData: [{
            required: true,
            message: "不能为空",
            trigger: "blur"
          }],
          scorePrinciple: [{
            required: true,
            message: "不能为空",
            trigger: "blur"
          }],
          scorePurpose: [{
            required: true,
            message: "不能为空",
            trigger: "blur"
          }],
          scoreSum: [{
            required: true,
            message: "不能为空",
            trigger: "blur"
          }],
          scoreTool: [{
            required: true,
            message: "不能为空",
            trigger: "blur"
          }]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询用户实验报告列表 */
      getList() {
        this.loading = true;

        getallExp().then(res => {
          this.allexps = res.data.map(exp => {
            return {name:exp.expName,id:exp.id}
          })
        })

        listReport(this.queryParams).then(response => {
          console.log(response)
          this.reportList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          "expId": undefined,
          "userId": undefined,
          "scorePurpose": undefined,
          "scoreTool": undefined,
          "scorePrinciple": undefined,
          "scoreContent": undefined,
          "scoreData": undefined,
          "scoreSum": undefined,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 提交按钮 */
      submitForm() {
        var params = {
          "expId": this.form.expId,
          "userId": this.form.userId,
          "scorePurpose": this.form.scorePurpose,
          "scoreTool": this.form.scoreTool,
          "scorePrinciple": this.form.scorePrinciple,
          "scoreContent": this.form.scoreContent,
          "scoreData": this.form.scoreData,
          "scoreSum": this.form.scoreSum
        }

        console.log(params)

        this.buttonLoading = true;

        expReportGetScore(params).then(res=>{
          this.$modal.msgSuccess("提交成功");
          this.open = false;
          this.getList();
          console.log(res)
        }).finally(() => {
          this.buttonLoading = false;
        });
      },
      /*打分*/
      Updatescore(row) {
        this.loading = true;
        this.reset();
        const id = row.id || this.ids

        getReport(id).then(response => {
          console.log(response)
          this.loading = false;
          this.form = response.data;
          this.open = true;
          this.title = "打分";
        });
        console.log(row) //获得该列数据
      },
      openurl(url) {
        window.open(url)
      },
      querySearch(queryString, cb) {
        var results =queryString? this.allexps.filter(this.createFilter(queryString)):this.allexps
        console.log(this.allexps)
        // 调用 callback 返回建议列表的数据
        cb(results);
      },
      createFilter(queryString) {
              return (restaurant) => {
                return (restaurant.name.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
              };
            },

      handleSelect(item) {
        this.tem.expName = item.name
        this.queryParams.expId = item.id
      },
      idToName(id){
        var index = this.allexps.findIndex(allexp => allexp.id == id)
        return this.allexps[index].name
      }






    }
  };
</script>

<style>
  .name {
        text-overflow: ellipsis;
        overflow: hidden;
      }
      .addr {
        font-size: 12px;
        color: #b4b4b4;
      }

</style>
